<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member_admin_module extends CI_Module {
    private   $data ;
    private   $sid ;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('member.User_model');

        $this->load->model('admin.Upload_model');
    }

    /**
     * 添加会员
     *
     *
     */

    public function add_member()
    {

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $array = $this->input->post('Form');
            $user_id = $this->input->post('user_id');
            if(empty($user_id))
            {
                $array['sid'] = $this->sid;
                $res = $this->User_model->_add_member($array) ;
                $failure_url = site_url('member/admin/add_member');
            }
            else
            {
                $where = array('user_id'=>$user_id);
                $res = $this->User_model->_edit_member($array,$where) ;
                $failure_url = site_url('member/admin/edit_member');
            }
            if($res)
            {
                $this->success('操作成功',site_url('member/admin/member_list'),true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }

        $this->data['form_url'] = site_url('/member/admin/add_member/');
        $this->data['check_username_uri'] = site_url('/member/index/verify_gateway/?action=username_ver') ;
        $this->data['check_mobile_uri'] = site_url('/member/index/verify_gateway/?action=mobile_ver') ;
        $this->load->view('/admin/view_add_member',$this->data);
    }

    /**
     * 添加积分规则
     *
     *
     */

    public function add_rule()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $array = $this->input->post('Form');
            $rid = $this->input->post('rid');
            if(empty($rid))
            {
                $array['sid'] = $this->sid;
                $res = $this->User_model->_add_rule($array) ;
                $failure_url = site_url('member/admin/add_rule');
            }
            else
            {
                $where = array('rid'=>$rid);
                $res = $this->User_model->_edit_rule($array,$where) ;
                $failure_url = site_url('member/admin/edit_rule');
            }

            if($res)
            {
                $this->success('操作成功',site_url('member/admin/integral_rule_list'),true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }
        $this->data['form_url'] = site_url('/member/admin/add_rule/');

        $this->load->view('/admin/view_add_integral_rule',$this->data);


    }

    /**
     * 编辑积分规则
     *
     *
     */

    public function edit_rule()
    {

        $rid = $this->uri->segment(4);
        $this->data=$this->User_model->_integral_rule_info($rid);
        $this->data['form_url'] = site_url('/member/admin/add_rule/');
        $this->load->view('/admin/view_edit_integral_rule',$this->data);
    }

    /**
     * 编辑会员
     *
     *
     */

    public function edit_member()
    {

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $user_id = $this->input->get_post('user_id');
            $where = array('user_id'=>$user_id);
            $base_data = $this->input->get_post('Form');
            $expand_data = $this->input->get_post('Expand');
            if(!empty($base_data['password']))
            {
                $ran_str = $this->User_model->_rand_6_char();
                $base_data['password'] = md5(md5($base_data['password'].$ran_str));
                $base_data['mix'] = $ran_str ;
            }
            $base_data = array_filter($base_data);
            $expand_data = array_filter($expand_data);
            if(!empty($base_data))
            {
                $res = $this->User_model->_edit_member($base_data,$where) ;
            }
            else
            {
                $res = TRUE ;
            }
            if(!empty($expand_data))
            {
                $res1 = $this->User_model->_edit_member_expand($expand_data,$where) ;
            }
            else
            {
                $res1 = TRUE ;
            }

            $failure_url = site_url('member/admin/edit_member/'.$user_id);

            if($res&&$res1)
            {
                $this->success('操作成功',site_url('member/admin/member_list'),true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }

        $user_id = $this->uri->segment(4);
        $this->data= $this->User_model->_user_info($user_id);
        $this->data['user_id'] = $user_id ;
        $this->data['form_url'] = site_url('/member/admin/edit_member');
        $this->data['check_email_uri'] = site_url('/member/index/verify_gateway/?user_id='.$user_id.'&action=email_ver') ;
        $this->data['check_mobile_uri'] = site_url('/member/index/verify_gateway/?user_id='.$user_id.'&action=mobile_ver') ;
        $this->load->view('/admin/view_edit_member',$this->data);

    }


    /**
     * 会员列表
     *
     *
     */

    public function member_list()
    {
        $sid = $this->sid;
        $where  = "sid = ".$sid  ;
        $condition['word'] = addslashes($this->input->get_post('word'));
        $condition['status'] = addslashes($this->input->get_post('status'));
        $condition['tl_status'] = addslashes($this->input->get_post('tl_status'));
        if($condition['word']){
            $where.= " AND (username LIKE '%".$condition['word']."%' OR email LIKE '%".$condition['word']."%' OR mobile LIKE '%".$condition['word']."%')";
        }
        if($condition['status']){
            $where .= " AND status = 0";
        }
        if($condition['tl_status']){
            $where .= " AND tl_status > 0";
        }
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='member/admin/member_list';//url
        $page_config['param'] = '?word='.$condition['word'] ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->User_model->get_member_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $member_array = $this->User_model->get_member_list($where,$page_config['perpage'],$page_config['nowindex']);
        $this->data['member_array'] = $member_array ;
        $this->data['form_url'] = site_url('member/admin/member_list');
        $this->data['condition'] = $condition;
        $this->load->view('/admin/view_member_list',$this->data);

    }


    /**
     * 会员详情
     *
     *
     */

    public function member_detail()
    {

        $this->load->view('/admin/view_member_detail',$this->data);

    }

    /**
     * 积分规则列表
     *
     *
     */

    public function integral_rule_list()
    {
        $page_config['perpage']=5;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='member/admin/integral_rule_list';//url
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->User_model->get_integral_rule_count(array(),'1');
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $rule_array = $this->User_model->get_integral_rule_list(array(),$page_config['perpage'],$page_config['nowindex'],'1');
        $this->data['rule_array'] = $rule_array ;
        $this->load->view('/admin/view_integral_rule_list',$this->data);

    }
    /**
     * 会员状态
     *
     *
     */

    public function member_status()
    {

        $post = $this->input->post();
        $data[$post['field']] = $post['val'];
        $res = $this->User_model->_edit_member($data,array('user_id'=>$post['id']));
        if($res){
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败','',true);
        }
    }

    /**
     * 删除会员
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_member()
    {

        $id = $this->input->get('id');
        if(empty($id)){
            $post_id = $this->input->post('id');
            $res = $this->User_model->_delete_member("user_id in ($post_id)");
        }else{

            $res = $this->User_model->_delete_member(array('user_id'=>$id));
        }
        if($res){
            $this->success('操作成功',site_url('member/admin/member_list'),true);
        }else{
            $this->error('操作失败','',true);
        }
    }


    /**
     * 删除积分规则
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_integral_rule()
    {

        $id = $this->input->get('id');
        if(empty($id)){
            $post_id = $this->input->post('id');
            $res = $this->User_model->_delete_rule("rid in ($post_id)");
        }else{

            $res = $this->User_model->_delete_rule(array('rid'=>$id));
        }
        if($res){
            $this->success('操作成功',site_url('member/admin/integral_rule_list'),true);
        }else{
            $this->error('操作失败','',true);
        }
    }



    public function upload_avatar()
    {
        $user_id = $this->input->get_post('uid');
        $config = $this->Upload_model->upload_config('avatar');
        $this->load->library('upload', $config);
        $absolute_path = str_replace('\\', '/', getcwd()) . '/';
        if (!$this->upload->do_upload('file')) {
            $error = array('error' => $this->upload->display_errors());
            $this->error($error['error'], '', true);
        } else {
            $data = array('upload_data' => $this->upload->data());
            $message = str_replace($absolute_path, '', $data['upload_data']['full_path']);
            $file_ext = $data['upload_data']['file_ext'];
            $dir_url = '/uploads/' . $GLOBALS['sid'] . '/avatar/';
            $des_url = $dir_url . $user_id . $file_ext;
            $des = getcwd() . $des_url;
            if (copy($message, $des)) {
                if (unlink($message)) {
                    $new_url = substr($des_url, 1);
                    $where = array('user_id' => $user_id);
                    $array['avatar'] = $new_url;
                    $res = $this->User_model->_edit_member_expand($array,$where);
                }
            }

            
           
            $this->success(URLPIC . $new_url.'?_='.time(), '', true);
        }
    }

    public function ajax_memeber()
    {
        $id = $this->input->get_post('id');
        if(!$id)  $this->error('数据异常','',true);
        $res = $this->User_model->_user_info($id);
        if(!empty($res['mobile']) || !empty($res['realname']) ){
            $array = array('mobile'=>$res['mobile'],'realname'=>$res['realname']);
            $this->success(json_encode($array),1,true);
        }
    }

}
